package wcg;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.LookAndFeel;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
import javax.swing.JLabel;
import java.awt.GridLayout;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.JComboBox;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Toolkit;
import javax.swing.JCheckBox;
import javax.swing.JSeparator;
import javax.swing.SwingConstants;
import java.awt.Label;
import java.awt.Color;
import javax.swing.border.BevelBorder;


public class DlgSystemOption extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JComboBox comboBox;
	public boolean RetCodeOk=false;
	private String lafOrig;
	public boolean LAFChange = false;
	private JCheckBox chckbxCheckUpdateOn;
	public boolean CheckUpdateOnStart=false;
	
	public String LookAndFeel;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			DlgSystemOption dialog = new DlgSystemOption();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public //private static String WinchkgLAFFile = "winchkglaf.ini";
	
	DlgSystemOption() {
		setIconImage(Toolkit.getDefaultToolkit().getImage(DlgSystemOption.class.getResource("/images/sprocket_dark.png")));
		setTitle("System Options");
		setBounds(100, 100, 331, 224);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		
		comboBox = new JComboBox();
		comboBox.setBounds(36, 31, 231, 31);
		
		LookAndFeel claf = UIManager.getLookAndFeel();
		String ctxt = claf.getName().trim();
		
		UIManager.LookAndFeelInfo[] installedLafs = UIManager.getInstalledLookAndFeels();
        for(int i=0; i<installedLafs.length; i++){
            UIManager.LookAndFeelInfo info=installedLafs[i];
            String txt = info.getName().trim();
            comboBox.addItem(txt);
        }
        comboBox.setSelectedItem(ctxt);
        lafOrig = ctxt;
	        
		
		contentPanel.add(comboBox);
		
		chckbxCheckUpdateOn = new JCheckBox("Check update on start");
		chckbxCheckUpdateOn.setBounds(36, 108, 180, 21);
		contentPanel.add(chckbxCheckUpdateOn);
		
		JLabel LabelUpdateTitileBorder = new JLabel();
		LabelUpdateTitileBorder.setBounds(12, 83, 281, 63);
		LabelUpdateTitileBorder.setBorder(new TitledBorder(null, "Update ", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		
		contentPanel.add(LabelUpdateTitileBorder);
		{
			JLabel label = new JLabel();
			label.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Look and Feel", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0)));
			label.setBounds(12, 5, 281, 73);
			contentPanel.add(label);
		}
		
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						doDataExchange(true);
						
//						try {
//							FileWriter fw = new FileWriter(WinchkgLAFFile);
//							BufferedWriter bw = new BufferedWriter(fw);
//							String laf = (String) comboBox.getSelectedItem();
//							bw.write(laf);
//							bw.flush();
//							fw.close();
//							bw.close();
//							if(lafOrig.compareTo(laf)!=0){
//								LAFChange = true;
//							}
//						} catch (FileNotFoundException e) {
//						} catch (IOException e) {
//						}
//						
						RetCodeOk=true;
						dispose();
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						dispose();
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
				getRootPane().setDefaultButton(cancelButton);
			}
		}
	}
	
	public void doDataExchange(boolean save){
		if(save){
			CheckUpdateOnStart = chckbxCheckUpdateOn.isSelected();
			LookAndFeel = new String((String) comboBox.getSelectedItem());
			if(lafOrig.compareTo(LookAndFeel)!=0){
				LAFChange = true;
			}
		}
		else{
			chckbxCheckUpdateOn.setSelected(CheckUpdateOnStart);
		}
	}
}
